package com.czy.owner.utils;

import android.Manifest;
import android.content.ContentProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;

import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.WebViewRequestParam;
import com.czy.owner.global.Constants;
import com.czy.owner.permission.Acp;
import com.czy.owner.permission.AcpListener;
import com.czy.owner.permission.AcpOptions;
import com.czy.owner.ui.order.ProjectDetailsWebviewActivity;
import com.czy.owner.ui.store.BindStoreDetailActivity;
import com.czy.owner.widget.UniversalDialog;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.List;

import static com.czy.owner.entity.WebViewRequestParam.REQUEST_POST;

/**
 * Created by hedepu on 2017-09-04
 */

public class IntentUtils {
    public static void onClickCallPhone(final Context content, final String phone) {
        if (StringUtils.isEmpty(phone)) {
            PhoneUtils.ShowToastMessage(content, "未获取到联系方式");
            return;
        }
        Acp.getInstance(content).request(new AcpOptions.Builder().setPermissions(Manifest.permission.CALL_PHONE).build(),
                new AcpListener() {
                    @Override
                    public void onGranted() {
                        ExitLoginDialog(content, phone, "呼叫", "取消");
                    }

                    @Override
                    public void onDenied(List<String> permissions) {
                        PhoneUtils.ShowToastMessage(content, permissions.toString() + "权限拒绝");

                    }
                });

    }

    private static void ExitLoginDialog(final Context content, final String title, String sure, String cancle) {
        new UniversalDialog(content).builder().setTitle(title)
//                .setMsg(msg)
                .setPositiveButton(sure, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_CALL);
                        intent.setData(Uri.parse("tel:" + title));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        content.startActivity(intent);

                    }
                }).setNegativeButton(cancle, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        }).show();
    }

    public static void startBulletin(Context content, String infoId) {
        WebViewRequestParam webViewRequestParam = new WebViewRequestParam(Constants.APP_API_BASE + Constants.OWN_BULLETIN);
        webViewRequestParam.setRequestMethod(REQUEST_POST);
        webViewRequestParam.setParams("session=" + UserHelper.getInstance().getSessionInfoModel(
                content).getSession() + "&storeId=" + (SharedPreferencesUtils.getParam(content, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "") +  "&infoId=" + infoId
        );
        MyLog.e("hedepu", "session=" + UserHelper.getInstance().getSessionInfoModel(
                content).getSession() +"&storeId=" + (SharedPreferencesUtils.getParam(content, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "") +  "&infoId=" + infoId
        );
        webViewRequestParam.setTitle("公告");
        Intent intent = new Intent(content, ProjectDetailsWebviewActivity.class);
        intent.putExtra(Constants.TRANSMISSION_CONTENT_KEY, webViewRequestParam);
        content.startActivity(intent);
    }
}
